<template>
  <div class="bigbox">
    <!-- 搜索 -->
    <div class="topnav">
      <div class="icon">
        <van-icon color="#fff" size="1.5rem" name="arrow-left" />
      </div>
      <van-search
        class="search"
        v-model="value"
        show-action
        background="#1cb676"
        placeholder="请输入小区或地址"
        @search="onSearch"
      >
        <template #label>
          <div @click="$router.push('/listcity')">
            <span ref="city">{{ city }}</span>
            <van-icon name="arrow-down" />
          </div>
        </template>
        <template #action>
          <div class="action" @click="onSearch">
            <van-icon color="#fff" size="1.5rem" name="map-marked" />
          </div>
        </template>
      </van-search>
    </div>
    <!-- 搜索 -->

    <!-- 省市区 -->
    <ul class="classfiy">
      <li>
        <van-cell @click="showPopup1">区域<i>▼</i> </van-cell>
        <van-popup
          position="top"
          v-model="show1"
          :style="{ width: '100%', height: '302px' }"
        >
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="onConfirm2"
            @cancel="onCancel2"
          >
            <!-- <template #cancel>
              <div>
                <button class="btn1" @click="popu1remove">取消</button>
              </div>
            </template>
            <template #confirm  @confirm="onConfirm2">
              <div>
                <button class="btn2">确认</button>
              </div>
            </template> -->
          </van-picker>
        </van-popup>
      </li>
      <!-- ----方式----- -->
      <li>
        <van-cell @click="showPopup2">方式<i>▼</i> </van-cell>
        <van-popup
          position="top"
          v-model="show2"
          :style="{ width: '100%', height: '40%' }"
        >
          <van-picker
            title="方式"
            show-toolbar
            :columns="rentType.arr"
            @confirm="onConfirm1"
            @cancel="onCancel1"
          />
        </van-popup>
      </li>
      <!-- -----租金------ -->
      <li>
        <van-cell @click="showPopup3">租金<i>▼</i> </van-cell>
        <van-popup
          position="top"
          v-model="show3"
          :style="{ width: '100%', height: '40%' }"
        >
          <van-picker
            title="租金"
            show-toolbar
            :columns="price.arr"
            @confirm="onConfirm"
            @cancel="onCancel"
          />
        </van-popup>
      </li>
      <!-- --------筛选--------- -->
      <li>
        <van-cell @click="showPopup4">筛选<i>▼</i> </van-cell>
        <van-popup
          position="right"
          v-model="show4"
          :style="{ width: '75%', height: '100%' }"
        >
          <van-row gutter="20">
            <p>户型</p>
            <div class="spbox">
              <span
                :class="{ btnpublic: all.indexOf(item) !== -1 }"
                @click="Screening(item, roomType.obj)"
                v-for="(item, index) in roomType.arr"
                :key="index"
              >
                {{ item }}
              </span>
            </div>
          </van-row>

          <van-row gutter="20">
            <p>朝向</p>
            <div class="spbox">
              <span
                :class="{ btnpublic: all.indexOf(item) !== -1 }"
                @click="Screening(item, oriented.obj)"
                v-for="(item, index) in oriented.arr"
                :key="index"
              >
                {{ item }}
              </span>
            </div>
          </van-row>

          <van-row gutter="20">
            <p>楼层</p>
            <div class="spbox">
              <span
                :class="{ btnpublic: all.indexOf(item) !== -1 }"
                @click="Screening(item, floor.obj)"
                v-for="(item, index) in floor.arr"
                :key="index"
              >
                {{ item }}
              </span>
            </div>
          </van-row>

          <van-row gutter="20">
            <p>房屋特点</p>
            <div class="spbox">
              <span
                :class="{ btnpublic: all.indexOf(item) !== -1 }"
                @click="Screening(item, characteristic.obj)"
                v-for="(item, index) in characteristic.arr"
                :key="index"
              >
                {{ item }}
              </span>
            </div>
            <button class="footbtn btnn1" @click="delAll">清除</button>
            <button class="footbtn btnn2" @click="popu4add">确认</button>
          </van-row>
        </van-popup>
      </li>
    </ul>
    <van-divider />
    <MyColRen v-for="(item,index) in list" :key="index" :item="item"></MyColRen>
  </div>
</template>

<script>
import { housesCond, ConditionHouses } from '@/api/article'
import { getCityname, getCitycode } from '@/utils/storage'
import MyColRen from '@/components/MyColRen.vue'
export default {
  components: {
    MyColRen
  },
  async created() {
    this.city = getCityname()
    const {
      body: {
        area: { children },
        characteristic,
        floor,
        oriented,
        price,
        rentType,
        roomType,
        subway
      }
    } = await housesCond(getCitycode())
    // console.log(subway)
    // 方式处理
    this.rentType = this.deal(rentType)
    // 租金处理
    this.price = this.deal(price)
    // 楼层处理
    this.floor = this.deal(floor)
    // 户型处理
    this.roomType = this.deal(roomType)
    // 朝向处理
    this.oriented = this.deal(oriented)
    // 特点处理
    this.characteristic = this.deal(characteristic)
    // 区域处理
    this.area = this.arealist(children)
    this.columns[0].children = this.columnslist(this.area)
    // 地铁处理
    this.subway = this.arealist(subway.children)
    this.columns[1].children = this.columnslist(this.subway)

    // 条件查房
    this.getHouses()
  },
  data() {
    return {
      // 房屋数据
      list: [],

      city: '',
      value: '',
      show1: false,
      show2: false,
      show3: false,
      show4: false,

      // 方式
      rentType: {},
      // 租金
      price: {},
      // 楼层
      floor: {},
      // 户型
      roomType: {},
      // 朝向
      oriented: {},
      // 特点
      characteristic: {},
      // 区域
      area: {},
      subway: {},
      columns: [
        {
          text: '区域',
          children: []
        },
        {
          text: '地铁',
          children: []
        }
      ],

      //筛选高亮
      all: [],
      // 筛选所有code
      everyall: [],
      // 区域方式租金所有code
      everyarp: []
    }
  },
  methods: {
    // 公共方法
    deal(val) {
      let obj = {}
      let arr = []
      val.forEach((i) => {
        arr.push(i.label)
        obj[i.label] = i.value
      })
      return { obj, arr }
    },
    // 区域
    arealist(val) {
      let area = {
        first: {
          arr: [],
          obj: {}
        },
        second: {
          newarr: {},
          newobj: {}
        }
      }
      val.forEach((i) => {
        area.first.arr.push(i.label)
        area.first.obj[i.label] = i.value
        if (i.children) {
          let key = i.label
          area.second.newarr[key] = []
          area.second.newobj[key] = {}
          i.children.forEach((j) => {
            area.second.newarr[key].push(j.label)
            area.second.newobj[key][j.label] = j.value
          })
        }
      })
      // console.log(area)
      return area
    },
    // 区域二次处理
    columnslist(val) {
      let newarr = []
      val.first.arr.forEach((i, index) => {
        let obj = {}
        if (index === 0) {
          newarr.push({ text: '不限', children: [{ text: '不限' }] })
        } else {
          obj.text = i
          obj.children = []
          let arr = val.second.newarr[i]
          // console.log(arr);
          for (let key in arr) {
            // console.log(arr[key]);
            let newobj = { text: arr[key] }
            obj.children.push(newobj)
          }
          newarr.push(obj)
        }
        // this.columns[0].children.push(obj)
      })
      // console.log(newarr);
      return newarr
    },
    // 弹框
    onSearch() {},
    showPopup1() {
      this.show1 = !this.show1
    },
    showPopup2() {
      this.show2 = !this.show2
    },
    showPopup3() {
      this.show3 = !this.show3
    },
    showPopup4() {
      this.show4 = !this.show4
    },

    popu4add() {
      this.getHouses()
      this.showPopup4()
    },
    // 租金
    onConfirm(value) {
      // console.log('租金', value, index)
      this.everyarp[2] = this.price.obj[value]
      this.getHouses()
      this.showPopup3()
    },
    onCancel() {
      this.showPopup3()
    },
    // 方式
    onConfirm1(value) {
      // console.log('方式', value, index)
      this.everyarp[1] = this.rentType.obj[value]
      this.getHouses()
      this.showPopup2()
    },
    onCancel1() {
      this.showPopup2()
    },
    // 区域
    onConfirm2(value) {
      let [, district, Community] = value
      // console.log(this.area);
      this.everyarp[0] = this.area.second.newobj[district][Community]
      this.getHouses()
      this.showPopup1()
    },
    onCancel2() {
      this.showPopup1()
    },
    // 筛选
    Screening(item, obj) {
      // console.log(item)
      if (this.all.indexOf(item) === -1) {
        this.all.push(item)
        this.everyall.push(obj[item])
      } else {
        this.all.splice(this.all.indexOf(item), 1)
        this.everyall.splice(this.everyall.indexOf(obj[item]), 1)
      }
    },
    // 删除所有筛选
    delAll() {
      this.all = []
      this.everyall = []
    },
    // 条件查房
    async getHouses() {
      let more = [...this.everyall, ...this.everyarp]
        const {body:{list}} = await ConditionHouses(getCitycode(), more.join(','))
        // console.log('有条件', list)
        this.list = list
    }
  },
}
</script>

<style lang="less" scoped>
.bigbox {
  margin-bottom: 100px;
}
.citybtn {
  width: 10vw;
  padding-left: 2.53333vw;
  line-height: 9.06667vw;
  text-align: center;
  height: 9.06667vw;
  background-color: #fff;
  font-size: 10px;
  color: rgb(68, 67, 67);
}
.footbtn {
  width: 50%;
  border: none;
  height: 50px;
}
.btnn1 {
  width: 40%;
  color: #21b97a;
  background-color: #fff;
}
.btnn2 {
  width: 60%;
  color: #fff;
  background-color: #21b97a;
}
.btnpublic {
  border: 1px solid #21b97a;
  color: #21b97a;
  background-color: #defaef;
}
.btn1 {
  width: 40%;
  position: absolute;
  color: #21b97a;
  background-color: #fff;
  border: none;
  bottom: 7px;
  left: 0;
  z-index: 999;
  height: 50px;
}
.btn2 {
  position: absolute;
  width: 60%;
  color: #fff;
  background-color: #21b97a;
  border: none;
  right: 0;
  bottom: 7px;
  z-index: 999;
  height: 50px;
}
.btnbox {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 50px;
  margin-top: 30px;
  justify-content: space-between;
  display: flex;
}
.spbox {
  // background-color: rgb(247, 243, 244);
  margin: 10px 10px 20px 40px;
  padding-bottom: 20px;
  border-bottom: 1px solid#ccc;
  span {
    border: 1px solid#ccc;
    display: inline-block;
    border-radius: 4px;
    width: 100px;
    height: 30px;
    line-height: 30px;
    margin-top: 10px;
    text-align: center;
  }
}
.van-popup--top {
  top: 7%;
}
.classfiy {
  display: flex;
  justify-content: space-evenly;
  margin: 0;
  padding: 0;
  li {
    width: 100px;
    height: 30px;
    p {
      margin-top: 20px;
      margin-bottom: 0px;
      padding: 0px 20px 0px 20px;
    }
    span {
      color: rgb(129, 126, 126);
      margin-left: 5px;
      font-size: 17px;
      transform: scale(0.8);
    }
  }
}
.topnav {
  display: flex;
  width: 100%;
  height: 3.125rem;
  background-color: #1cb676;
  .search {
    margin-top: 0.425rem;
    width: 90%;
    height: 2.125rem;
    border-radius: 0.325rem;
    overflow: hidden;
  }
  .icon {
    width: 2rem;
    line-height: 3.5rem;
    text-align: center;
  }
  .action {
    padding-top: 5px;
  }
}
.unselected {
  display: flex;
  height: 40px;
  border-bottom: 1px solid #e4e6f0;
  overflow: hidden;
  li {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 25%;
    height: 40px;
  }
  .active {
    color: #21b97a;
  }
}
</style>
